package cn.iocoder.yudao.module.infra.controller.admin.category;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.infra.controller.admin.category.vo.*;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTablePageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTableRespVO;
import cn.iocoder.yudao.module.infra.convert.TreeConvert;
import cn.iocoder.yudao.module.infra.convert.category.CategoryConvert;
import cn.iocoder.yudao.module.infra.convert.codegen.CodegenConvert;
import cn.iocoder.yudao.module.infra.dal.dataobject.category.CategoryDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO;
import cn.iocoder.yudao.module.infra.service.category.CategoryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "数据管理 - 分类编辑")
@RestController
@RequestMapping("/infra/category")
@Validated
public class CategoryController {

    @Resource
    private CategoryService categoryService;


    @GetMapping("/list")
    @Operation(summary = "获得表定义分页")
    @PreAuthorize("@ss.hasPermission('infra:category:query')")
    public CommonResult<List<CategoryTableRespVO>> getCategoryList(@Valid CategoryListVO pageReqVO) {
        List<CategoryDO> list = categoryService.getCategoryList(pageReqVO);
        return success(CategoryConvert.INSTANCE.convertPage(list));
    }
    @GetMapping("/get")
    @Operation(summary = "获得表定义分页")
    @PreAuthorize("@ss.hasPermission('infra:category:query')")
    public CommonResult<CategoryTableRespVO> getCategory(@RequestParam("id") Long id) {
        CategoryTableRespVO category = categoryService.getCategory(id);
        return success(category);
    }

    @PostMapping("/create")
    @Operation(summary = "创建分类")
    @PreAuthorize("@ss.hasPermission('infra:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody CategorySaveReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分类")
    @PreAuthorize("@ss.hasPermission('infra:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody CategorySaveReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        return success(categoryService.deleteCategory(id));
    }


    @GetMapping("/list-all-simple")
    @Operation(summary = "获取分类精简信息列表", description = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<CategorySimpleRespVO>> getSimpleDeptList() {
        CategoryListVO vo = new CategoryListVO();
        vo.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<CategoryDO> list = categoryService.getAllSimpleCategoryList(vo);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(CategoryDO::getSort));
        return success(CategoryConvert.INSTANCE.convertList02(list));
    }

    @GetMapping("/list-tree-simple")
    @Operation(summary = "获取分类精简信息列表", description = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<CategorySimpleRespVO>> getSimpleCategoryList(CategoryListVO pageReqVO) {
        List<CategorySimpleRespVO> tree = categoryService.tree(pageReqVO);
        TreeConvert<CategorySimpleRespVO> treeConvert = new TreeConvert<>();
        List<CategorySimpleRespVO> result = new ArrayList<>();
        treeConvert.findNodeByCode(tree,pageReqVO.getCode(),result);
        return success(result);
    }

    /**
     * 根据Code查询分类
     * @param code
     * @return
     */
    @GetMapping("{code}/select")
    public CommonResult<List<CategoryDO>> select(@PathVariable("code") String code){
       return success(categoryService.getCategoryByCode(code));
    }

}
